﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using G.Web;
using G.WeiBo.WebAPI.Response;
using G.Net;
using System.Text;

namespace G.WeiBo.WebAPI.Request
{
    public class UpdateStatueRequeste : IRequest<ResponseData<G.WeiBo.WebAPI.Domain.Message>>
    {
        //string content, float lat, float lng, string annotations, string in_reply_to_status_id = null
        public string Content { get; set; }

        public float? Lat { get; set; }

        public float? Lng { get; set; }

        public string ReplyStatusId { get; set; }
        /// <summary>
        /// 元数据，主要是为了方便第三方应用记录一些适合于自己使用的信息。
        /// 每条微博可以包含一个或者多个元数据。请以json字串的形式提交，字串长度不超过512个字符，具体内容可以自定。 
        /// </summary>
        public string Annotations { get; set; }

        public string GetURL()
        {
            return "statuses/update.json";
        }

        public string HttpMethod()
        {
            return G.Net.HttpMethod.Post;
        }

        public System.Collections.Generic.IDictionary<string, string> GetParameters()
        {
            LocalDictionary parameters = new LocalDictionary();
            parameters.Add("status", Content);
            parameters.Add("in_reply_to_status_id", ReplyStatusId);
            parameters.Add("lat", Lat);
            parameters.Add("long", Lng);
            parameters.Add("annotations", Annotations);
            return parameters;
        }
        public static string UrlEncode(string input)
        {
            StringBuilder newBytes = new StringBuilder();
            var urf8Bytes = Encoding.UTF8.GetBytes(input);
            foreach (var item in urf8Bytes)
            {
                if (IsReverseChar((char)item))
                {
                    newBytes.Append('%');
                    newBytes.Append(ByteToHex(item));

                }
                else
                    newBytes.Append((char)item);
            }

            return newBytes.ToString();
        }
        private static bool IsReverseChar(char c)
        {
            return !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
                    || c == '-' || c == '_' || c == '.' || c == '~');
        }

        private static string ByteToHex(byte b)
        {
            return b.ToString("x2");
        }
    }
}
